package com.kou.dao;

import com.kou.pojo.*;
import com.kou.utils.DataSourceUtil;
import com.kou.utils.RandomUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * @author SteinY
 * @create 2022-03-26 10:55
 * @description:
 */
public class OrderDaoImpl implements OrderDao {
    QueryRunner queryRunner  =new QueryRunner();
    @Override
    public String create(String uid, String aid, String sum) {
        Connection connection = DataSourceUtil.getConnection();
        String sql = "insert into tb_order values (?,?,?,?,?,?)";
        String oid = RandomUtils.createOrderId();
        Object[] params = {oid,Integer.parseInt(uid),Integer.parseInt(sum),"1",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()),Integer.parseInt(aid)};
        try {
            queryRunner.update(connection,sql,params);
            return oid;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return "";
    }

    @Override
    public List<Orders> findAll(String uid) {
        List<Orders> orders = new ArrayList<>();
        Connection connection = DataSourceUtil.getConnection();

        String sql = "SELECT * FROM tb_address ta INNER JOIN tb_order tr ON tr.aid=ta.id WHERE tr.uid = ? ";
        try {
            List<Map<String, Object>> maps = queryRunner.query(connection, sql, new MapListHandler(), uid);
            for (Map<String, Object> map : maps) {
                Orders order = new Orders();
                Address address = new Address();
                BeanUtils.copyProperties(order,map);
                BeanUtils.copyProperties(address,map);
                order.setAddress(address);
                orders.add(order);
            }
            return orders;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Orders findById(String oid) {
        Connection connection = DataSourceUtil.getConnection();
        String sql = "SELECT * FROM tb_order tr INNER JOIN tb_orderdetail td ON tr.id = td.oid INNER JOIN tb_goods tg ON td.pid=tg.id INNER JOIN tb_address ta ON ta.id = tr.aid  WHERE tr.id = ? ";
        try {
            List<Map<String, Object>> maps = queryRunner.query(connection, sql, new MapListHandler(), oid);
            List<OrderDetail> orderDetails = new ArrayList<>();
            Orders orders = new Orders();
            Address address = new Address();
            for (Map<String, Object> map : maps) {
                Goods goods = new Goods();
                OrderDetail orderDetail = new OrderDetail();

                BeanUtils.copyProperties(goods,map);
                BeanUtils.copyProperties(orderDetail,map);
                BeanUtils.copyProperties(orders,map);
                BeanUtils.copyProperties(address,map);
                orderDetail.setGoods(goods);
                orderDetails.add(orderDetail);

            }
            orders.setAddress(address);
            orders.setOrderDetails(orderDetails);
            return orders;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void createDetail(String oid, List<Cart> carts) {
        Connection connection = DataSourceUtil.getConnection();
        String sql = "insert into tb_orderdetail values (0,?,?,?,?)";
        try {
            for (Cart cart : carts) {
                queryRunner.update(connection,sql,oid,cart.getPid(),cart.getNum(),cart.getMoney());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Orders> show() {
        Connection connection = DataSourceUtil.getConnection();
        String  sql = "select * from tb_order ";
        try {
            return queryRunner.query(connection,sql,new BeanListHandler<>(Orders.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void send(String oid) {
        Connection connection = DataSourceUtil.getConnection();
        String  sql = "update tb_order set status = 3 where id = ? ";
        try {
            queryRunner.update(connection,sql,oid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Orders> find(int uid, String status) {
        Connection connection = DataSourceUtil.getConnection();
        String  sql = "select * from tb_order where uid = ? and status = ? ";
        try {
            return queryRunner.query(connection,sql,new BeanListHandler<>(Orders.class),uid,Integer.parseInt(status));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Orders> showAll() {
        Connection connection = DataSourceUtil.getConnection();
        String  sql = "select * from tb_order ";
        try {
            return queryRunner.query(connection,sql,new BeanListHandler<>(Orders.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
